//
//  SNFoodClass.m
//  project
//
//  Created by 黄建华 on 2018/11/15.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "SNFoodClass.h"

@implementation SNFoodClass

DEF_SINGLETON(SNFoodClass)

- (instancetype)init {
    self = [super init];
    if (self) {
        _foodArray = [NSMutableArray array];
        _bmrCalor = 0;
        
    }
    return self;
}

- (NSString*)firstCreate
{
    NSString *dateString = [[NSDate date]dateToDayString];
    
    if (SN_USER.first_meal_date.length >0) {
        dateString = SN_USER.first_meal_date;
    }
//    else
//    {
//        NSString *where = [NSString stringWithFormat:@"dateString <='%@' and currentCalor >0",[[NSDate date]dateToDayString]];
//        NSArray *foodArray = [FoodModel searchWithWhere:where orderBy:@"dateString" offset:-1 count:0];
//
//        for (FoodModel *model in foodArray) {
//            if (model.count >0) {
//                dateString = model.dateString;
//                break;
//            }
//        }
//    }
  
    return dateString;
}

- (NSInteger )foodCounts
{
    NSInteger count = 0;
    NSString *where = [NSString stringWithFormat:@"dateString <='%@' and currentCalor >0 and user_id ='%@'",[[NSDate date]dateToDayString],SN_USER.user_id];
    NSArray *foodArray = [FoodModel searchWithWhere:where orderBy:@"dateString" offset:-1 count:0];
    count = foodArray.count;
    return count;
}

- (NSInteger)bmrCalor
{
    return SN_PublicClass.todayFood.goal_calory;
}
- (void)addFood:(NSDictionary *)dic withName:(NSString *)name
{
    BOOL add = NO;
    for (NSDictionary *dic in _foodArray) {
         NSString *foodName = dic[@"name"];
        if ([foodName isEqualToString:name]) {
            add = YES;
            break;
        }
    }
    if (!add) {
        [_foodArray addObject:dic];
    }
}

- (NSDictionary *)getFoodWithName:(NSString *)foodName
{
    NSDictionary *dic = @{@"name":foodName,@"state":@"1",@"food_id":@"1",@"calory":@"1.0",@"food_alias_id":@"1"};
    for (NSDictionary *dict in _foodArray) {
        NSString *Name = dict[@"name"];
        if ([Name isEqualToString:foodName]) {
            dic = dict;
            break;
        }
    }
    return dic;
}

- (NSArray *)foodUnitArray
{
    return @[SN_Text(@"g"),SN_Text(@"lb"),SN_Text(@"oz"),SN_Text(@"ml"),];
}

- (CGFloat)getMultipleWithUnit:(NSString *)weightUnit
{
    CGFloat multiple = 1.0;
    if ([weightUnit isEqualToString:@""]) {
        multiple = 0.0;
    }
    if ([weightUnit isEqualToString:SN_Text(@"g")]) {
            multiple = 1.0;
    }
    if ([weightUnit isEqualToString:SN_Text(@"Kg")]) {
            multiple = 1000.0;
    }
    if ([weightUnit isEqualToString:SN_Text(@"lb")]) {
        multiple = 453.59237;
    }
    if ([weightUnit isEqualToString:SN_Text(@"oz")]) {
        multiple = 28.3495231;
    }
    if ([weightUnit isEqualToString:SN_Text(@"ml")]) {
        multiple = 1.0;
    }
    return multiple;
}

// 同步服务器上的菜单列表
- (void)uploadFoodListForService
{
    NSInteger localFoodListCount = self.foodCounts;
    NSLog(@"第一次使用食谱时间>>>%@ 本地菜单列表数量>>>%d  服务器菜单列表数量>>>%d",SN_USER.first_meal_date,localFoodListCount,SN_USER.total_meal_day);
    
//    localFoodListCount = 0;
    if (SN_USER.total_meal_day > 0) {
        if (localFoodListCount < SN_USER.total_meal_day) {
            NSLog(@"同步历史食谱");
            [SN_Net foodListSearchWithStateDate:self.firstCreate Success:^(id object) {
                
            } fail:^(id object) {
                
            }];
        }
        else
        {
            if (localFoodListCount == SN_USER.total_meal_day) {
                NSString *todayString = SN_PublicClass.todayFood.dateString;
                [SN_Net foodListSearchWithDate: todayString Success:^(id object) {
                    NSDictionary *dic = object[@"data"];
                    [FoodModel updateModelWithDic:dic withDateString:todayString];
                } fail:^(id object) {
                }];
            }
        }
    }
}

@end
